Imports


In [62]:
import math as m # math library
import numpy as np
import scipy as sp
import pandas as pd
import os
my_plotly_api_key = os.environ.get('MY_PLOTLY_API_KEY')
# setting up a key:
# in Terminal:
# export MY_PLOTLY_API_KEY = 'DHAHH3DSAD43D' (EXAMPLE)

import plotly
plotly.tools.set_credentials_file(username='agu3rra', api_key=my_plotly_api_key) # setting up credentials; Plotly is an online service.
import plotly.plotly as py # import graphics library
import plotly.graph_objs as go
from scipy import integrate

In [7]:
#Functions

In [33]:
def f1(y, x): # mind the y, x order; inner most integral comes first.
    return (3 + x**2 - 2*y) # 3+x^2-2y
def bound_y(x):
    return [-x,x]
def bound_x():
    return [0.0,1.0]

In [32]:
integrate.nquad(f1, [bound_y, bound_x])


Out[32]:
(3.4999999999999996, 8.85288647725923e-14)

In [26]:
integrate.dblquad(lambda x, y: 3 + x**2 - 2*y, 1.0, 0.0, lambda x: x, lambda x: -x) # Wrong! As dy is the first inner most integral, the first lamdba call must be as the one below.


Out[26]:
(1.8333333333333333, 2.960584255223384e-14)

In [28]:
integrate.dblquad(lambda y, x: 3 + x**2 - 2*y, 0.0, 1.0, lambda x: -x, lambda x: x)


Out[28]:
(3.4999999999999996, 8.85288647725923e-14)

Understanding meshgrids


In [43]:
x = np.linspace(0.0,1.0,num=10)
y = np.linspace(-1.0,1.0,num=5)

In [45]:
xv, yv = np.meshgrid(x, y)

In [46]:
x


Out[46]:
array([ 0.        ,  0.11111111,  0.22222222,  0.33333333,  0.44444444,
        0.55555556,  0.66666667,  0.77777778,  0.88888889,  1.        ])

In [47]:
y


Out[47]:
array([-1. , -0.5,  0. ,  0.5,  1. ])

In [48]:
xv


Out[48]:
array([[ 0.        ,  0.11111111,  0.22222222,  0.33333333,  0.44444444,
         0.55555556,  0.66666667,  0.77777778,  0.88888889,  1.        ],
       [ 0.        ,  0.11111111,  0.22222222,  0.33333333,  0.44444444,
         0.55555556,  0.66666667,  0.77777778,  0.88888889,  1.        ],
       [ 0.        ,  0.11111111,  0.22222222,  0.33333333,  0.44444444,
         0.55555556,  0.66666667,  0.77777778,  0.88888889,  1.        ],
       [ 0.        ,  0.11111111,  0.22222222,  0.33333333,  0.44444444,
         0.55555556,  0.66666667,  0.77777778,  0.88888889,  1.        ],
       [ 0.        ,  0.11111111,  0.22222222,  0.33333333,  0.44444444,
         0.55555556,  0.66666667,  0.77777778,  0.88888889,  1.        ]])

In [49]:
yv


Out[49]:
array([[-1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. ],
       [-0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5],
       [ 0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ,  0. ],
       [ 0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5],
       [ 1. ,  1. ,  1. ,  1. ,  1. ,  1. ,  1. ,  1. ,  1. ,  1. ]])

Parametric plotting


In [63]:
# https://plot.ly/python/3d-parametric-plots/
x = np.linspace(0.0,1.0,num=50)
y = np.linspace(-1.0,1.0,num=100)

In [64]:
xGrid, yGrid = np.meshgrid(x,y)

In [65]:
z = 3 + xGrid**2 - 2*yGrid

In [67]:
surface = go.Surface(x=x, y=y, z=z)
data = [surface]

layout = go.Layout(
    title='Parametric Plot',
    scene=dict(
        xaxis=dict(
            gridcolor='rgb(255, 255, 255)',
            zerolinecolor='rgb(255, 255, 255)',
            showbackground=True,
            backgroundcolor='rgb(230, 230,230)'
        ),
        yaxis=dict(
            gridcolor='rgb(255, 255, 255)',
            zerolinecolor='rgb(255, 255, 255)',
            showbackground=True,
            backgroundcolor='rgb(230, 230,230)'
        ),
        zaxis=dict(
            gridcolor='rgb(255, 255, 255)',
            zerolinecolor='rgb(255, 255, 255)',
            showbackground=True,
            backgroundcolor='rgb(230, 230,230)'
        )
    )
)

fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='Parametric_plot')


Out[67]:

In [80]:
x = np.linspace(-10.0,10.0,num=50)
y = np.linspace(-10.0,10.0,num=50)
xGrid, yGrid = np.meshgrid(x,y)
z = np.sin(xGrid**2 + yGrid**2) / (xGrid**2 + yGrid**2)

In [81]:
surface = go.Surface(x=x, y=y, z=z)
data = [surface]

layout = go.Layout(
    title='Parametric Plot',
    scene=dict(
        xaxis=dict(
            gridcolor='rgb(255, 255, 255)',
            zerolinecolor='rgb(255, 255, 255)',
            showbackground=True,
            backgroundcolor='rgb(230, 230,230)'
        ),
        yaxis=dict(
            gridcolor='rgb(255, 255, 255)',
            zerolinecolor='rgb(255, 255, 255)',
            showbackground=True,
            backgroundcolor='rgb(230, 230,230)'
        ),
        zaxis=dict(
            gridcolor='rgb(255, 255, 255)',
            zerolinecolor='rgb(255, 255, 255)',
            showbackground=True,
            backgroundcolor='rgb(230, 230,230)'
        )
    )
)

fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='Parametric_plot')


Out[81]:

In [ ]: